﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using MangoCannon.Framework.Socket;

namespace MangoCannon.Framework.Base
{
    /// <summary>
    /// 客户端会话（可序列存储）
    /// 1、当用户正常进入时创建会话，退出时关闭会话
    /// 2、当用户非正常退出时保留会话，用户重新进入时取回会话
    /// 3、服务器实例退出不影响会话，系统退出时会关闭所有会话
    /// </summary>
    [DataContract]
    public class ClientSession
    {
        /// <summary>
        /// 一次性临时ID
        /// </summary>
        private long sessionId = 0;

        public long SessionId
        {
            get {
                if (sessionId == 0)
                {
                    byte[] buffer = Guid.NewGuid().ToByteArray();
                    return BitConverter.ToInt64(buffer, 0);
                }
                else
                {
                    return sessionId;
                }
            }
        }

        [DataMember]
        public ClientHandle sessionOriginalClient { get; set; }
        [DataMember]
        public Dictionary<string, object> session { get; set; }
    }
}
